<template>
  <div id="login">
    <div class="left-box">
      <div class="title-box">
        <img src="@/assets/logo.png" alt="" />
        <span class="title">黑马面面</span>
        <span class="line"></span>
        <span class="sub-title">用户登录</span>
      </div>
      <!-- 表单内容 -->
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号"
            prefix-icon="el-icon-user"
            class="my-input"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            class="my-input"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="17"
              ><el-input
                v-model="form.code"
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
                class="my-input"
              ></el-input
            ></el-col>
            <el-col :span="7">
              <img
                class="code-img"
                :src="codeUrl"
                alt=""
                @click="changeCodeUrl"
              />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="checked">
          <el-checkbox v-model="form.checked"
            >我已阅读并同意<el-link type="primary" :underline="false"
              >用户协议</el-link
            >和<el-link type="primary" :underline="false">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="my-btn" @click="formSubmit('form')"
            >登录</el-button
          >
          <el-button type="primary" class="my-btn" @click="goRegister"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <img src="@/assets/login_banner_ele.png" alt="" />
    <!-- 注册页面 -->
    <register ref="register"></register>
  </div>
</template>

<script>
// 导入注册页面
import register from '../register/index.vue'
// 导入登录请求api
import { login } from '../../api/login.js'
// 导入Token工具
import { setToken } from '../../utils/token.js'

export default {
  name: 'login',
  components: {
    // 注册
    register
  },
  data () {
    return {
      form: {
        phone: '',
        checked: [],
        password: '',
        code: ''
      },
      rules: {
        phone: [
          {
            required: true,
            message: '手机号不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '手机号格式有误',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '密码不能为空',
            trigger: 'blur'
          },
          {
            min: 6,
            max: 12,
            message: '密码长度为 6 到 12',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '验证码不能为空',
            trigger: 'blur'
          },
          {
            min: 4,
            max: 4,
            message: '验证码长度为4',
            trigger: 'blur'
          }
        ],
        checked: [
          {
            // 必须有内容,而不是布尔值
            required: true,
            message: '必须同意用户协议',
            trigger: 'blur'
          }
        ]
      },
      codeUrl: process.env.VUE_APP_BaseURL + '/captcha?type=login'
    }
  },
  methods: {
    // 变更验证码
    changeCodeUrl () {
      this.codeUrl =
        process.env.VUE_APP_BaseURL + '/captcha?type=login&t=' + Math.random()
    },
    // 注册按钮
    goRegister () {
      // 利用ref 和 $refs来传递参数
      this.$refs.register.dialogFormVisible = true
    },
    formSubmit (form) {
      this.$refs[form].validate(valid => {
        if (valid) {
          // alert('submit!')
          const { phone, password, code } = this.form
          console.log(phone, password, code)
          login({ phone, password, code }).then(res => {
            // console.log(res)
            this.$message.success('登录成功')
            // 设置Token
            setToken(res.data.data.token)
            // 登录跳转
            this.$router.push('/index')
          })
        } else {
          console.log('error submit!!')
          this.$message.error('登录失败,请重试')

          return false
        }
      })
    }
  }
}
</script>

<style lang="less">
#login {
  height: 100vh;
  // background-color: tomato;
  background: linear-gradient(#01c6fa 28%, #1493fa 96%);
  display: flex;
  align-items: center;
  justify-content: space-around;
  .left-box {
    box-sizing: border-box;
    padding: 48px 42px 0;
    width: 478px;
    height: 550px;
    opacity: 1;
    background: #f5f5f5;
    .title-box {
      margin-bottom: 28px;
      padding-left: 5px;
      display: flex;
      align-items: center;
      img {
        margin-top: 4px;
        margin-right: 13px;
      }
      .title {
        font-size: 24px;
      }
      .line {
        margin: 0 13px;
        height: 28px;
        width: 1px;
        background-color: #c7c7c7;
      }
      .sub-title {
        font-size: 22px;
      }
    }
    .my-input > input {
      height: 45px;
      line-height: 45px;
    }
    .my-btn {
      margin-bottom: 26px;
      width: 100%;
    }
    .el-button + .el-button {
      margin-left: 0;
    }
    .el-form-item__content {
      line-height: 0;
    }
    .code-img {
      // display: block;
      box-sizing: border-box;
      padding: 4px;
      width: 100%;
      height: 45px;
      cursor: pointer;
    }
  }
}
</style>
